package security.Authorization.Permission;

import security.Authentication.Base.IdentifiedUser;
import security.Authentication.Base.UnknownUser;
import security.Authentication.Base.User;
import security.Authorization.Base.AuthorizationHandler;
import security.Authorization.Base.AuthorizationLevel;

public class PermissionBased implements AuthorizationHandler {
	
	public boolean hasPermission(IdentifiedUser identifiedUser){
		if(identifiedUser.getLevel() == AuthorizationLevel.ADMIN){
			return true;
		}
		return false;
	}
	
	public void openAccount(IdentifiedUser identifiedUser){
		if (hasPermission(identifiedUser) ||identifiedUser.getLevel() == AuthorizationLevel.USER){
			//allow open account
		} else {
			//deny operation
		}	
	}
	
	public void deleteData(IdentifiedUser identifiedUser){
		if (hasPermission(identifiedUser)){
			//allow deletion
		} else {
			//deny operation
		}
	}
	
	public void editAccount(IdentifiedUser identifiedUser){
		if (hasPermission(identifiedUser) ||identifiedUser.getLevel() == AuthorizationLevel.USER){
			//allow edit account
		} else {
			//deny operation
		}
	}
	
	public void seePage(IdentifiedUser identifiedUser){
		if(identifiedUser.getLevel() == AuthorizationLevel.NONE){
			//deny operation
		} else {
			// allow see page
		}
	}

	@Override
	public void authorize(User user) {
		if (user instanceof IdentifiedUser) {
			IdentifiedUser identifiedUser = (IdentifiedUser)user;
			this.hasPermission(identifiedUser);
		} else if (user instanceof UnknownUser) {
			UnknownUser unknownUser = (UnknownUser)user;
			System.out.println("Show nothing because: " + unknownUser.getCause());
		}
	}
}
